<template>
  <div class="card-container">
    <a-card size="small">
      <template #title>
        <div class="title">
          <component v-if="props.icon" :style="{ fontSize: '18px', color: token.colorPrimary }" />
          <slot name="title"></slot>
          <div v-if="!$slots.title">
            <a-tabs class="title-content" style="font-weight: 700" v-if="props.leftTabs" v-model:activeTab="activeTab" @change="handleLeftTabChange">
              <a-tab-pane v-for="tab in props.leftTabs" :key="tab.key" :tab="tab.title" />
            </a-tabs>
            <span v-else class="title-content">{{ props.title }}</span>
          </div>
        </div>
      </template>
      <template #extra>
        <slot name="extra"></slot>
        <div v-if="!$slots.extra" class="extra-content">
          <a-select v-if="props.dropdown" v-model="selectedValue" placeholder="请选择" style="width: 120px" @change="handleSearchChange" allowClear>
            <a-select-option v-for="option in props.dropdown" :key="option.value" :value="option.value">
              {{ option.label }}
            </a-select-option>
          </a-select>
          <a-tabs v-if="props.tabs" v-model:activeTab="activeTab" @change="handleTabChange">
            <a-tab-pane v-for="tab in props.tabs" :key="tab.key" :tab="tab.title" />
          </a-tabs>
          <a v-if="props.extra" @click="extraClick">{{ props.extra }}</a>
        </div>
      </template>
      <slot></slot>
    </a-card>
  </div>
</template>

<script setup lang="ts">
import { theme } from 'ant-design-vue'
import { ref } from 'vue'

const props = defineProps({
  icon: String,
  title: String,
  extra: String,
  leftTabs: Array, // 左侧的 tabs
  tabs: Array, // 右侧的 tabs
  dropdown: Array
})

const { useToken } = theme
const { token } = useToken()

const activeTab = ref(1)
const selectedValue = ref('')

function handleTabChange(key: string) {
  emit('tabChange', key)
}

function handleLeftTabChange(key: string) {
  emit('leftTabChange', key)
}

function handleSearchChange(value: string) {
  emit('searchChange', value)
}

function extraClick() {
  emit('extraClick')
}

const emit = defineEmits(['tabChange', 'leftTabChange', 'searchChange', 'extraClick'])
</script>

<style lang="less" scoped>
.card-container {
  height: 100%;

  .title {
    display: flex;
    align-items: center;
    position: relative;
    height: 20px;
    &::before {
      content: '';
      position: absolute;
      top: 3px;
      left: 0;
      width: 3px;
      height: 30px;
      background-color: v-bind('token.colorPrimary');
    }
  }

  .title-content {
    margin-left: 10px;
    font-size: 14px;
    color: v-bind('token.colorText');
  }

  .extra-content {
    display: flex;
    align-items: center;
    gap: 10px;
  }
}
</style>
